package com.demo.solution;

public class Solution2 {


    public String convert(String s, int numRows) {
        if (numRows <= 1) {
            return s;
        }
        StringBuilder str = new StringBuilder();
        int n = 2 * numRows - 2;
        for (int i = 0; i < numRows; i++) {
            if (i == 0 || i == numRows - 1) {
                for (int m = 0; m * n + i < s.length(); m++) {
                    str.append(s.charAt(m * n + i));
                }
            } else {
                int j = n - i;
                for (int m = 0; m * n + i < s.length(); m++) {
                    str.append(s.charAt(m * n + i));
                    if (m * n + j < s.length()) {
                        str.append(s.charAt(m * n + j));
                    }
                }
            }
        }
        return new String(str);
    }


    public static void main(String[] args) {
        String s = "PAYPALISHIRING";
        String convert = new Solution2().convert("", 2);
        System.out.println("convert = " + convert);
    }
}